<template>
  <el-card class="wide-card" >
    <h2>基本信息</h2>
    <el-form ref="form" :model="savePeopleForm" label-width="100px "size="large"
             style="margin: 20px 0; padding: 0 20px;" >
      <el-form-item prop="nickname" label="用户昵称">
        <el-input v-model="savePeopleForm.nickname" placeholder="请输入昵称" ></el-input>
      </el-form-item>
      <el-form-item prop="gender" label="性别">
        <el-select v-model="savePeopleForm.gender" placeholder="请选择性别" >
          <el-option label="男" value="male"></el-option>
          <el-option label="女" value="female"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="personalSummary" label="个人简介">
        <el-input v-model="savePeopleForm.personalSummary" placeholder="留下点什么话吧"></el-input>
      </el-form-item>
      <el-form-item prop="addressManagement" label="地址管理">
        <el-input v-model="savePeopleForm.addressManagement" placeholder="快递送到这"></el-input>
      </el-form-item>
      <el-form-item prop="birthDate" label="出生日期">
        <el-date-picker v-model="savePeopleForm.birthDate" type="date" placeholder="选择出生日期" style="width: 200px;"></el-date-picker>
      </el-form-item>

      <el-button color="#0089BA" style="width: 200px; margin: 20px auto;" @click="savePeople" >保存</el-button>
    </el-form>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from 'vue';
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
const user= ref(getUser());
//定义保存用户的信息
//定义对象保存查询条件
const savePeopleForm = ref({
  avatar:user.value.avatar,
  nickname:user.value.nickname,
  email:user.value.email,
  personalSummary:'',
  addressManagement:'',
  birthDate:'',
  gender:''
});
//加载个人信息数据
const loadForm = ()=>{
  let data = qs.stringify(savePeopleForm.value);
  axios.get(BASE_URL+'/v1/user/select?'+data).then((response)=>{
    if (response.data.code==2000){
      savePeopleForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

onMounted(()=> {
  loadForm();//一进入页面就需要加载个人信息
})
//保存个人信息
const savePeople = ()=>{
  savePeopleForm.value.id = user.value.id;
  console.log(savePeopleForm.value)
  let data = qs.stringify(savePeopleForm.value);
  console.log(data);
  axios.post(BASE_URL+'/v1/user/save',data).then((response)=>{
    if (response.data.code == 2000){
      ElMessage.success('保存成功!');
      savePeopleForm.value={};
      loadForm();
    }else{
      ElMessage.error('保存失败！');
    }
  })
}



</script>

<style scoped>
.wide-card {
  width: 85%; /* 调整卡片宽度 */
  margin: 0 auto; /* 居中显示 */
}
</style>
